#include<stdio.h>
#include<malloc.h>

/* 
Implement List Functions : 
1) Append 
2) Extend 
3) Pop
4) Insert 
5) remov 
6) Sort 
7) Reverse 
8) Count 
9) index

as : 
(a) Dynamic Array
/// -> ?!!?!!?!?!?!!?!?!?! (b) Linked List
*/ 

//typedef struct n{
int count;
int *l;
void  init(){
  count = 0;
  l = NULL;
}
void *append(int val){
  int *temp = malloc(sizeof(int)*(count+1)),i;
  for(i = 0;i<count;i++){
    temp[i] = l[i];
  }
  temp[i] = val;
  l = temp;
  count++;
}
void extend(){
  int n,i;
  printf("How many elements to extend? : ");
  scanf("%d",&n);
  for(i = 0;i<n;i++){
    printf("Enter element : ");
    int temp;
    scanf("%d",&temp);
    append(temp);
  }    
}
int pop(int loc){
  if(loc>count){
    printf("IndexError : pop index out of range\n");
    return -1;
  }
  else{
    int i = loc,ans = l[loc];
    for(i;i<count;i++){
    l[i] = l[i+1];
    }
    count--;
    return ans;
  }
}
void insert(int loc, int val){
  if(loc > count) loc = count;
  //printf("Inserting at : %d\n",loc);
  int i=0;
  int *temp = malloc(sizeof(int)*(count+1));
  for(i=0;i<loc;i++){
    temp[i] = l[i];
  }
  temp[i] = val;
  //printf("Now at : %d\n",i);
  for(i;i<count;i++) temp[i+1] = l[i];
  count+=1;
  l = temp;
}
int remov(int val){
  int flag=1,i;
  for(i=0;i<count;i++){
    if(l[i] == val){
      flag = 0;
      break;
    }
  }
  if(flag) return -1;
  else{
    for(i;i<count;i++)
    l[i] = l[i+1];
    count--;
    return 0;
  }
}
void sort(){
  int i,j;
  for(i = 0;i<count;i++)
    for(j=i+1;j<count;j++)
      if(l[i]>l[j]){
        int temp = l[i];
        l[i] = l[j];
        l[j] = temp;
      }
}
void reverse(){
  int *temp = malloc(sizeof(int)*count),i;
  for(i = 0;i<count;i++){
    temp[i] = l[count-i-1];
  }
  l = temp;
}
int ind(int val){
  int i;
  for(i=0;i<count;i++)
    if(l[i] == val)
      return i;
  return -1;
}
  


void print(){
  int i;
  printf("[");
  for(i = 0;i<count;i++){
    printf("%d",l[i]);
    if(i!=(count-1)) printf(",");
  }
  printf("]\n");
}

int main(void){
  //alist l;
  int a,b,c,d;
  init();
  while(1){
    printf("Array Lists : \n1. Insert\n2. Append\n3. Extend\n4. Pop\n5. Remove\n6. Index\n7. Reverse\n8. Sort\n9. Length\n10. Exit\n?: ");
    scanf("%d",&a);
    switch(a){
      case 1: 
        printf("Enter value to insert : ");
        scanf("%d",&b);
        printf("Enter index : ");
        scanf("%d",&c);
        insert(c,b);
        break;
      case 2:
        printf("Enter Value to append: ");
        scanf("%d",&b);
        append(b);
        break;
      case 3:
        extend();
        break;
      case 4:
        printf("Enter index to pop : ");
        scanf("%d",&b);
        d = pop(b);
        if(d == -1) break;
        else printf("Value popped : %d",d);
        break;
      case 5:
        printf("Enter Value to remove : ");
        scanf("%d",&b);
        d = remov(b);
        if(d == -1){
          printf("ValueError : list.remove(x) : x not in list\n");
        }
        break;
      case 6:
        printf("Enter Value to find : ");
        scanf("%d",&b);
        d = ind(b);
        if(d == -1) printf("ValueError : list.index(x) : x not in list\n");
        else printf("Index : %d",d);
        break;
      case 7:
        reverse();
        break;
      case 8:
        sort();
        break;
      case 9:
        printf("Length of the list is : %d",count);
        break;
      case 10:
        return 0;
      default: 
        printf("Select from among the given options!");
    }
    printf("\n");
    print(l);
  }
  return 0;
}
